home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Panorama / Panorama - Disk 28C (1988-04-27)(Pacific North-West Amigas Club)[WB].zip / Panorama - Disk 28C (1988-04-27)(Pacific North-West Amigas Club)[WB].adf / ModulaII / Etchm2 / EtchAsketch.Mod next >
Text File  |  1987-12-24  |  6KB  |  204 lines

  1. MODULE EtchAsketch;
  2. (*********************************************************)
  3. (*                    EtchAsketch                        *)
  4. (*                                                       *)
  5. (*    A program to demonstrate the use of the joystick   *)
  6. (*             device and hardware sprites.              *)
  7. (*                                                       *)
  8. (*        Written for the Benchmark M2 compiler.         *)
  9. (*                                                       *)
  10. (*  Steve Faiwiszewski                    December 1987  *)
  11. (*                                                       *)
  12. (*********************************************************)
  13.  
  14. FROM EtchGlobal   IMPORT AddTerminationProc, ExitGracefully,
  15.                          MyVPort, MyRPort, ChipAllocate;
  16. FROM EtchJoystick IMPORT OpenJoystick, GetJoystickStatus,
  17.                          ListenToJoystick,  Left, Right,
  18.                          PrepareToReadJoystick,
  19.                          SetTriggerTime, Forward, Backward;
  20. FROM EtchIntuiStuff
  21.                  IMPORT ListenToIntuition, SetSpriteColors,
  22.                         ProcessIntuiMessages;
  23. FROM Tasks       IMPORT SignalSet, Wait;
  24. FROM Drawing     IMPORT Move, Draw, SetAPen,PolyDraw, SetDrMd,
  25.                         RectFill, ReadPixel;
  26. FROM Sprites     IMPORT SimpleSprite, GetSprite, ChangeSprite,
  27.                         FreeSprite, MoveSprite, AnySprite;
  28. FROM Gels        IMPORT InitGels, GelsInfoPtr, VSprite,
  29.                         VSpritePtr;
  30. FROM Rasters     IMPORT RastPortPtr;
  31. FROM TermInOut   IMPORT WriteString, WriteLn;
  32. FROM SYSTEM      IMPORT TSIZE;
  33.  
  34. CONST
  35.    SpriteHeight = 5;
  36.    SpriteCenterOffset = (SpriteHeight DIV 2);
  37.    Xinc = 1;
  38.    Yinc = 1;
  39.    Xmin = SpriteCenterOffset;
  40.    Ymin = 5;
  41.    Xmax = 320 - Xmin;
  42.    Ymax = 190 - Ymin;
  43.  
  44. TYPE
  45.     DirectionType = (up,down,left,right);
  46.     DirectionSet  = SET OF DirectionType;
  47.  
  48.     SpriteImageBuf = RECORD
  49.         data : ARRAY[0..SpriteHeight+1],[0..1] OF CARDINAL;
  50.     END;
  51.  
  52. VAR
  53.     SpriteImagePtr : POINTER TO SpriteImageBuf;
  54.     MySignalSet    : SignalSet;
  55.     MySprite       : SimpleSprite;
  56.     CurX, CurY,
  57.     StolenSprite   : INTEGER;
  58.  
  59. PROCEDURE GetDirection(VAR NewDirection : DirectionSet;
  60.                        VAR ButtonDown : BOOLEAN);
  61. (* Get Joystick directions and button press *)
  62. VAR
  63.     Ystick,
  64.     Xstick : INTEGER;
  65. BEGIN
  66.     GetJoystickStatus(ButtonDown,Xstick,Ystick);
  67.     NewDirection := DirectionSet{};
  68.     CASE Ystick OF
  69.         Forward  : INCL(NewDirection,up)        |
  70.         Backward : INCL(NewDirection,down)
  71.         ELSE    (* do nothing *)
  72.     END; (* case *)
  73.     CASE Xstick OF
  74.         Left  : INCL(NewDirection,left)         |
  75.         Right : INCL(NewDirection,right)
  76.         ELSE    (* do nothing *)
  77.     END; (* case *)
  78. END GetDirection; 
  79.  
  80. PROCEDURE Allowed(NewDirection : DirectionType) : BOOLEAN;
  81. (* Check if requested movement is allowed *)
  82. BEGIN
  83.     CASE NewDirection OF
  84.         up   : RETURN CurY >= Ymin + Yinc                |
  85.         down : RETURN CurY <= Ymax - Yinc                |
  86.         left : RETURN CurX >= Xmin + Xinc                |
  87.         right: RETURN CurX <= Xmax - Xinc
  88.         ELSE
  89.                RETURN FALSE
  90.     END; (* case *)
  91. END Allowed;
  92.  
  93.  
  94. PROCEDURE MovePlayer(NewDirection : DirectionSet;
  95.                      ButtonDown : BOOLEAN);
  96. (* Move the sprite in the requested direction *)
  97. (* only if move is legal.                     *)
  98. VAR
  99.     dir : DirectionType;
  100. BEGIN
  101.     IF NewDirection <> DirectionSet{} THEN
  102.         FOR dir := up TO right DO
  103.             IF (dir IN NewDirection) AND Allowed(dir) THEN
  104.                 CASE dir OF
  105.                     up   : DEC(CurY,Yinc)  |
  106.                     down : INC(CurY,Yinc)  |
  107.                     left : DEC(CurX,Xinc)  |
  108.                     right: INC(CurX,Xinc)
  109.                 END;
  110.             END; (* if *)
  111.         END; (* for *)
  112.         IF ButtonDown THEN
  113.             Draw(MyRPort^,CurX + SpriteCenterOffset,
  114.                           CurY + SpriteCenterOffset);
  115.         ELSE
  116.             Move(MyRPort^,CurX + SpriteCenterOffset,
  117.                           CurY + SpriteCenterOffset);
  118.         END;
  119.         MoveSprite(MyVPort^,MySprite,CurX,CurY);
  120.     END;
  121. END MovePlayer;
  122.  
  123. PROCEDURE LoopAround;
  124. (* Listen for Joystick and Intuition Events, *)
  125. (* and process them.                         *)
  126. VAR
  127.     sig : SignalSet;
  128.     Directions : DirectionSet;
  129.     exiting,
  130.     ButtonDown : BOOLEAN;
  131. BEGIN
  132.     exiting := FALSE;
  133.     ButtonDown := FALSE;
  134.     REPEAT
  135.         PrepareToReadJoystick;
  136.         sig := Wait(MySignalSet);
  137.         ProcessIntuiMessages(exiting);
  138.         GetDirection(Directions,ButtonDown);
  139.         MovePlayer(Directions,ButtonDown)
  140.     UNTIL exiting;
  141. END LoopAround;
  142.  
  143. PROCEDURE InitSpriteImage;
  144. (* Set up the sprite's image.  Must be in Chip RAM. *)
  145. VAR
  146.     i : CARDINAL;
  147. BEGIN
  148.     ChipAllocate(SpriteImagePtr,TSIZE(SpriteImageBuf));
  149. (* We only need to initialize non-zero data, as  *)
  150. (* ChipAllocate initialized the allocated memory *)
  151.     WITH SpriteImagePtr^ DO
  152.         data[1,0] := 2000H;
  153.         data[2,0] := 7000H;        data[2,1] := 2000H;
  154.         data[3,0] := 0D800H;        data[3,1] := 5000H;
  155.         data[4,0] := 7000H;        data[4,1] := 2000H;
  156.         data[5,0] := 2000H;
  157.     END; (* with *)
  158. END InitSpriteImage;
  159.  
  160. PROCEDURE PrepareSprite;
  161. (* Obtain a hardware sprite and set it up *)
  162. BEGIN
  163.     InitSpriteImage;
  164.     StolenSprite := GetSprite(MySprite,AnySprite);
  165.     IF StolenSprite = -1 THEN
  166.         WriteString('** Could not obtain sprite! **');
  167.         WriteLn;
  168.         ExitGracefully
  169.     END;
  170.     SetSpriteColors(StolenSprite);
  171.     CurX := (Xmax - Xmin) DIV 2 + Xmin;
  172.     CurY := (Ymax - Ymin) DIV 2 + Ymin;
  173.     WITH MySprite DO
  174.         height := SpriteHeight;
  175.         x := CurX;
  176.         y := CurY;
  177.     END;
  178.     ChangeSprite(MyVPort^,MySprite,SpriteImagePtr);
  179.     Move(MyRPort^,CurX + SpriteCenterOffset,
  180.                   CurY + SpriteCenterOffset);
  181. END PrepareSprite;
  182.  
  183. PROCEDURE cleanup;
  184. (* Release the hardware sprite *)
  185. BEGIN
  186.     FreeSprite(StolenSprite)
  187. END cleanup;
  188.  
  189. BEGIN
  190.     PrepareSprite;
  191.     AddTerminationProc(cleanup);
  192.     OpenJoystick;
  193.     MySignalSet := SignalSet{};
  194.     ListenToIntuition(MySignalSet);
  195.     ListenToJoystick(MySignalSet);
  196.     IF NOT SetTriggerTime(1) THEN
  197.         WriteString('** error setting trigger **');
  198.         WriteLn;
  199.         ExitGracefully
  200.     END;
  201.     LoopAround;
  202.     ExitGracefully
  203. END EtchAsketch.
  204.